package asia.doing.service.front;

import asia.doing.entity.*;
import asia.doing.mapper.ArticleMapper;
import asia.doing.mapper.CategoryMapper;
import asia.doing.mapper.TagMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service(value = "frontArticleService")
public class ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private TagMapper tagMapper;

    public void addViews(Integer articleId){
        Article article = new Article();
        article.setId(articleId);
        article.setViews(1); //设置views，让它不为空
        articleMapper.updateViews(article);
    }


    public List<Article> selectArticle(Article article) {
        //不管有没有id，查询就完事了
        List<Article> articles = articleMapper.selectAll(article);
        for (Article articleItem : articles) {
            int articleId = articleItem.getId();
            ArticleCategory articleCategory;
            articleCategory = new ArticleCategory();
            articleCategory.setArticleId(articleId);
            List<ArticleCategory> articleCategories = categoryMapper.selectByArtOrCaId(articleCategory);
            HashSet<Integer> categoryIds = new HashSet<>();
            for (ArticleCategory articleCategoryItewm : articleCategories) {
                if(articleId == articleCategoryItewm.getArticleId()){
                    categoryIds.add(articleCategoryItewm.getCategoryId());
                }
            }
            //拿到菜单ids
            Integer[] categoryIdsArray = categoryIds.toArray(new Integer[categoryIds.size()]);
            //开始查询一个个菜单
            List<Category> categories = new ArrayList<>();
            for (Integer i : categoryIdsArray) {
                List<Category> categories1 = categoryMapper.selectAll(new Category(i));
                categories.add(categories1.get(0));
            }



            ArticleTag articleTag;
            articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            List<ArticleTag> articleTags = tagMapper.selectByArtOrTagId(articleTag);
            HashSet<Integer> tagIds = new HashSet<>();
            for (ArticleTag articleTagItewm : articleTags) {
                if(articleId == articleTagItewm.getArticleId()){
                    tagIds.add(articleTagItewm.getTagId());
                }
            }
            Integer[] tagIdsArray = tagIds.toArray(new Integer[tagIds.size()]);
            //开始查询一个个菜单
            List<Tag> tags = new ArrayList<>();
            for (Integer i : tagIdsArray) {
                List<Tag> tags1 = tagMapper.selectAll(new Tag(i));
                tags.add(tags1.get(0));
            }


            //将分类和标签添加到article类中
            articleItem.setCategoryList(categories);
            articleItem.setTagList(tags);
        }
        return articles;
    }


    public PageInfo<Article> selectPage(Article article, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //注意:这里获取到的信息是重复的
        List<Article> list = articleMapper.selectAll(article);
        PageInfo<Article> pageInfo = PageInfo.of(list);
        List<Article> articles = pageInfo.getList(); //可以获取所有列表循环做其他事情,这个传递地址值
        //查询之后进行遍历，每个Article对象中有两个数组，一个是categoryIds，一个是tagIds
        for (Article articleItem : articles) {
            int articleId = articleItem.getId();
            ArticleCategory articleCategory;
            articleCategory = new ArticleCategory();
            articleCategory.setArticleId(articleId);
            List<ArticleCategory> articleCategories = categoryMapper.selectByArtOrCaId(articleCategory);
            HashSet<Integer> categoryIds = new HashSet<>();
            for (ArticleCategory articleCategoryItewm : articleCategories) {
                if(articleId == articleCategoryItewm.getArticleId()){
                    categoryIds.add(articleCategoryItewm.getCategoryId());
                }
            }
            //拿到菜单Id
            Integer[] categoryIdsArray = categoryIds.toArray(new Integer[categoryIds.size()]);
            //开始查询一个个菜单
            List<Category> categories = new ArrayList<>();
            for (Integer i : categoryIdsArray) {
                List<Category> categories1 = categoryMapper.selectAll(new Category(i));
                categories.add(categories1.get(0));
            }


            ArticleTag articleTag;
            articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            List<ArticleTag> articleTags = tagMapper.selectByArtOrTagId(articleTag);
            HashSet<Integer> tagIds = new HashSet<>();
            for (ArticleTag articleTagItewm : articleTags) {
                if(articleId == articleTagItewm.getArticleId()){
                    tagIds.add(articleTagItewm.getTagId());
                }
            }
            //拿到标签Id
            Integer[] tagIdsArray = tagIds.toArray(new Integer[tagIds.size()]);
            //开始查询一个个菜单
            List<Tag> tags = new ArrayList<>();
            for (Integer i : tagIdsArray) {
                List<Tag> tags1 = tagMapper.selectAll(new Tag(i));
                tags.add(tags1.get(0));
            }


            //将分类和标签添加到article类中
            articleItem.setCategoryList(categories);
            articleItem.setTagList(tags);
        }

        //根据views进行排序
        if(article.getSort() != null){
            Collections.sort(articles, new Comparator<Article>() {
                @Override
                public int compare(Article a1, Article a2) {
                    // Sort in descending order (highest views first)
                    return Integer.compare(a2.getViews(), a1.getViews());
                }
            });
        }

        return pageInfo;
    }
}
